﻿/*
        Copyright (C) 2007  Aaron Defazio

        This program is free software; you can redistribute it and/or
        modify it under the terms of the GNU General Public License
        as published by the Free Software Foundation; either version 2
        of the License, or (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
using System;
using System.IO;
using System.Net;
using System.Security;
using System.Security.Permissions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using AppLn;
using System.Windows.Media.Imaging;
using System.Drawing;

namespace BlinkLn
{
	public partial class App: System.Windows.Application
	{
        Controller Cn;       
        System.Windows.Forms.NotifyIcon test;
        HotKey hotkey;

        protected override void OnStartup(StartupEventArgs e)
        {
            this.ShutdownMode = ShutdownMode.OnExplicitShutdown;

            Window splash = new Splash();
            this.MainWindow = splash;
            splash.Show();

            Cn = new Controller();
            
            //Setup system tray stuff
            test = new System.Windows.Forms.NotifyIcon();
            test.Icon = new System.Drawing.Icon("Blink.ico");
            test.Click += new EventHandler(test_Click);
            test.Visible = true;

            test.ContextMenu = new System.Windows.Forms.ContextMenu();
            test.ContextMenu.MenuItems.Add("Quit");
            test.ContextMenu.MenuItems[0].Click += new EventHandler(QuitMenuItem_Click);

            hotkey = new HotKey();
            hotkey.RegisterHotKey();
            hotkey.Pressed +=new EventHandler(hotkey_Pressed);

            splash.Close();

        }

        void hotkey_Pressed(object sender, EventArgs e)
        {           
            OpenWindow();
        }

        void QuitMenuItem_Click(object sender, EventArgs e)
        {
            Cn.close();
            test.Visible = false;
            this.Shutdown();
        }

        void test_Click(object sender, EventArgs e)
        {
            OpenWindow();
        }

        void OpenWindow()
        {
            if (this.MainWindow == null || this.MainWindow.IsLoaded == false)
            {
                Window bln = new MainWindow(this.Cn);
                this.MainWindow = bln;
                bln.ShowDialog();
            }

        }
	}
}
